<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"kuang-yudi.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Gemini","version":"8.1.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/myblog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="grid网格布局">
<meta property="og:type" content="article">
<meta property="og:title" content="CSS新布局display:grid">
<meta property="og:url" content="http://kuang-yudi.gitee.io/myblog/2021/01/06/CSS%E6%96%B0%E5%B8%83%E5%B1%80display-grid/index.html">
<meta property="og:site_name" content="KY迪的个人博客">
<meta property="og:description" content="grid网格布局">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-01-06T12:57:56.000Z">
<meta property="article:modified_time" content="2021-01-06T14:07:39.162Z">
<meta property="article:author" content="匡宇迪">
<meta property="article:tag" content="CSS">
<meta property="article:tag" content="grid布局">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://kuang-yudi.gitee.io/myblog/2021/01/06/CSS%E6%96%B0%E5%B8%83%E5%B1%80display-grid/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>CSS新布局display:grid | KY迪的个人博客</title>
  



  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">KY迪的个人博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">个人技术博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#CSS-grid-%E7%BD%91%E6%A0%BC%E5%B8%83%E5%B1%80"><span class="nav-number">1.</span> <span class="nav-text">CSS grid 网格布局</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="nav-number">1.1.</span> <span class="nav-text">1. 概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">1.2.</span> <span class="nav-text">2. 基本概念</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-%E5%AE%B9%E5%99%A8%E5%92%8C%E9%A1%B9%E7%9B%AE"><span class="nav-number">1.2.1.</span> <span class="nav-text">2.1 容器和项目</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-%E8%A1%8C%E3%80%81%E5%88%97%E3%80%81%E5%8D%95%E5%85%83%E6%A0%BC%E3%80%81%E7%BD%91%E6%A0%BC%E7%BA%BF"><span class="nav-number">1.2.2.</span> <span class="nav-text">2.2 行、列、单元格、网格线</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E8%AE%BE%E7%BD%AE%E7%BD%91%E6%A0%BC%E5%B8%83%E5%B1%80-%E5%AE%B9%E5%99%A8%E5%B1%9E%E6%80%A7"><span class="nav-number">1.3.</span> <span class="nav-text">3. 设置网格布局-容器属性</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-%E5%BC%80%E5%90%AF%E7%BD%91%E6%A0%BC%E5%B8%83%E5%B1%80"><span class="nav-number">1.3.1.</span> <span class="nav-text">3.1 开启网格布局</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-%E8%AE%BE%E7%BD%AE%E8%A1%8C%E9%AB%98%E3%80%81%E5%88%97%E5%AE%BD"><span class="nav-number">1.3.2.</span> <span class="nav-text">3.2 设置行高、列宽</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-%E8%AE%BE%E7%BD%AE%E8%A1%8C%E9%97%B4%E8%B7%9D%E3%80%81%E5%88%97%E9%97%B4%E8%B7%9D"><span class="nav-number">1.3.3.</span> <span class="nav-text">3.3 设置行间距、列间距</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-grid-template-areas-%E5%B1%9E%E6%80%A7%E6%8C%87%E5%AE%9A%E5%8C%BA%E5%9F%9F"><span class="nav-number">1.3.4.</span> <span class="nav-text">3.4 grid-template-areas 属性指定区域</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-grid-auto-flow-%E6%8C%87%E5%AE%9A%E5%AE%B9%E5%99%A8%E7%9A%84%E5%AD%90%E5%85%83%E7%B4%A0%E7%9A%84%E6%8E%92%E6%94%BE%E9%A1%BA%E5%BA%8F"><span class="nav-number">1.3.5.</span> <span class="nav-text">3.5 grid-auto-flow 指定容器的子元素的排放顺序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%AD%E5%86%85%E5%AE%B9%E7%9A%84%E4%BD%8D%E7%BD%AE"><span class="nav-number">1.3.6.</span> <span class="nav-text">3.6 单元格中内容的位置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-7-%E5%AE%B9%E5%99%A8%E4%B8%AD%E5%86%85%E5%AE%B9%E7%9A%84%E4%BD%8D%E7%BD%AE"><span class="nav-number">1.3.7.</span> <span class="nav-text">3.7 容器中内容的位置</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E8%AE%BE%E7%BD%AE%E7%BD%91%E6%A0%BC%E5%B8%83%E5%B1%80-%E9%A1%B9%E7%9B%AE%E5%B1%9E%E6%80%A7"><span class="nav-number">1.4.</span> <span class="nav-text">4. 设置网格布局-项目属性</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-%E6%8C%87%E5%AE%9A%E9%A1%B9%E7%9B%AE%E4%BD%8D%E7%BD%AE"><span class="nav-number">1.4.1.</span> <span class="nav-text">4.1 指定项目位置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-%E6%8C%87%E5%AE%9A%E9%A1%B9%E7%9B%AE%E6%94%BE%E5%9C%A8%E5%93%AA%E4%B8%AA%E5%8C%BA%E5%9F%9F"><span class="nav-number">1.4.2.</span> <span class="nav-text">4.2 指定项目放在哪个区域</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-%E6%8C%87%E5%AE%9A%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%AD%E5%86%85%E5%AE%B9%E4%BD%8D%E7%BD%AE-%E5%8D%95%E4%B8%AA%E9%A1%B9%E7%9B%AE%E4%B8%AD"><span class="nav-number">1.4.3.</span> <span class="nav-text">4.3 指定单元格中内容位置(单个项目中)</span></a></li></ol></li></ol></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="匡宇迪"
      src="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
  <p class="site-author-name" itemprop="name">匡宇迪</p>
  <div class="site-description" itemprop="description">用于记录学习web前端</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">87</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/tonykyd/tonykyd.github.io" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tonykyd&#x2F;tonykyd.github.io" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1303665509@qq.com" title="E-Mail → mailto:1303665509@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://kuang-yudi.gitee.io/myblog/2021/01/06/CSS%E6%96%B0%E5%B8%83%E5%B1%80display-grid/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
      <meta itemprop="name" content="匡宇迪">
      <meta itemprop="description" content="用于记录学习web前端">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="KY迪的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          CSS新布局display:grid
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-01-06 20:57:56 / 修改时间：22:07:39" itemprop="dateCreated datePublished" datetime="2021-01-06T20:57:56+08:00">2021-01-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/myblog/categories/CSS/" itemprop="url" rel="index"><span itemprop="name">CSS</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

            <div class="post-description">grid网格布局</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="CSS-grid-网格布局"><a href="#CSS-grid-网格布局" class="headerlink" title="CSS grid 网格布局"></a>CSS grid 网格布局</h2><h3 id="1-概述"><a href="#1-概述" class="headerlink" title="1. 概述"></a>1. 概述</h3><p>Grid 布局指定容器内部多个项目的位置，将容器划分成”行”和”列”，产生单元格，然后指定”项目所在”的单元格</p>
<h3 id="2-基本概念"><a href="#2-基本概念" class="headerlink" title="2. 基本概念"></a>2. 基本概念</h3><h4 id="2-1-容器和项目"><a href="#2-1-容器和项目" class="headerlink" title="2.1 容器和项目"></a>2.1 容器和项目</h4><p>指定一个元素为容器，则它的顶层子元素为项目(grid 布局只对项目有效)</p>
<h4 id="2-2-行、列、单元格、网格线"><a href="#2-2-行、列、单元格、网格线" class="headerlink" title="2.2 行、列、单元格、网格线"></a>2.2 行、列、单元格、网格线</h4><p>容器内水平区域是行，垂直为列，行与列交叉区域为单元格(cell)<br>划分网格的线：网格线(grid line)</p>
<h3 id="3-设置网格布局-容器属性"><a href="#3-设置网格布局-容器属性" class="headerlink" title="3. 设置网格布局-容器属性"></a>3. 设置网格布局-容器属性</h3><h4 id="3-1-开启网格布局"><a href="#3-1-开启网格布局" class="headerlink" title="3.1 开启网格布局"></a>3.1 开启网格布局</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">元素&#123;</span><br><span class="line">  display:grid</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意，设为网格布局以后，容器子元素（项目）的 float、display:inline-block、display: table-cell、vertical-align 和 column-*等设置都将失效。</p>
<h4 id="3-2-设置行高、列宽"><a href="#3-2-设置行高、列宽" class="headerlink" title="3.2 设置行高、列宽"></a>3.2 设置行高、列宽</h4><p>grid-template-columns 属性定义每一列的列宽，grid-template-rows 属性定义每一行的行高。<br>例：3 行 3 列，每列、每行宽高为 100px</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.container &#123;</span><br><span class="line">  display: grid;</span><br><span class="line">  grid-template-columns: 100px 100px 100px;</span><br><span class="line">  grid-template-rows: 100px 100px 100px;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>repeat()</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.container &#123;</span><br><span class="line">  display: grid;</span><br><span class="line">  grid-template-columns: repeat(3, 33.33%);</span><br><span class="line">  grid-template-rows: repeat(3, 33.33%);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>repeat(重复次数，每次重复的值)</p>
<p><strong>auto-fill 关键字</strong><br>单元格的大小是固定的，但是容器的大小不确定。如果希望每一行（或每一列）容纳尽可能多的单元格，这时可以使用 auto-fill 关键字表示自动填充。<br>例：每列宽度 100px，然后自动填充，直到容器不能放置更多的列</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.container &#123;</span><br><span class="line">  display: grid;</span><br><span class="line">  grid-template-columns: repeat(auto-fill, 100px);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>fr 关键字</strong><br>表示比例关系，网格布局提供了 fr 关键字（fraction 的缩写，意为”片段”）。如果两列的宽度分别为 1fr 和 2fr，就表示后者是前者的两倍。</p>
<p>例：两个相同宽度的列(容器分为两份，项目各占 1 份)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.container &#123;</span><br><span class="line">  display: grid;</span><br><span class="line">  grid-template-columns: 1fr 1fr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>minmax(最小值，最大值)</strong><br>产生一个长度范围，表示长度就在这个范围之中。<br>例：第三列列宽不小于 100px，不大于 1fr</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grid-template-columns: 1fr 1fr minmax(100px, 1fr);</span><br></pre></td></tr></table></figure>

<p><strong>auto 关键字</strong></p>
<p>auto 关键字表示由浏览器自己决定长度。<br>例：第二列的宽度，基本上等于该列单元格的最大宽度</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grid-template-columns: 100px auto 100px;</span><br></pre></td></tr></table></figure>

<h4 id="3-3-设置行间距、列间距"><a href="#3-3-设置行间距、列间距" class="headerlink" title="3.3 设置行间距、列间距"></a>3.3 设置行间距、列间距</h4><p>grid-row-gap 属性设置行与行的间隔（行间距），grid-column-gap 属性设置列与列的间隔（列间距）</p>
<p>grid-gap 属性是 grid-column-gap 和 grid-row-gap 的合并简写形式，语法如下。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grid-gap: 行间距值 列间距值;</span><br></pre></td></tr></table></figure>

<h4 id="3-4-grid-template-areas-属性指定区域"><a href="#3-4-grid-template-areas-属性指定区域" class="headerlink" title="3.4 grid-template-areas 属性指定区域"></a>3.4 grid-template-areas 属性指定区域</h4><p>一个区域由单个或多个单元格组成</p>
<p>区域名字相同则相当于合并单元格</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">grid-template-areas: &#39;a a a&#39;</span><br><span class="line">                     &#39;b b b&#39;</span><br><span class="line">                     &#39;c c c&#39;;</span><br></pre></td></tr></table></figure>

<h4 id="3-5-grid-auto-flow-指定容器的子元素的排放顺序"><a href="#3-5-grid-auto-flow-指定容器的子元素的排放顺序" class="headerlink" title="3.5 grid-auto-flow 指定容器的子元素的排放顺序"></a>3.5 grid-auto-flow 指定容器的子元素的排放顺序</h4><p>默认值是 row，即”先行后列”。也可以将它设成 column，变成”先列后行”。<br>还可以 设置成 row dense 和 column dense。这两个值主要用于，某些项目指定位置以后，剩下的项目怎么自动放置。<br>row dense 表示”先行后列”，并且尽可能紧密填满，尽量不出现空格。<br>column dense 表示”先列后行”，并且尽量填满空格。</p>
<h4 id="3-6-单元格中内容的位置"><a href="#3-6-单元格中内容的位置" class="headerlink" title="3.6 单元格中内容的位置"></a>3.6 单元格中内容的位置</h4><p>justify-items 属性设置单元格内容的水平位置（左中右），align-items 属性设置单元格内容的垂直位置（上中下）。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.container &#123;</span><br><span class="line">  justify-items: start | end | center | stretch;</span><br><span class="line">  align-items: start | end | center | stretch;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>place-items 属性是 align-items 属性和 justify-items 属性的合并简写形式。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">place-items: &lt;align-items&gt; &lt;justify-items&gt;;</span><br></pre></td></tr></table></figure>

<h4 id="3-7-容器中内容的位置"><a href="#3-7-容器中内容的位置" class="headerlink" title="3.7 容器中内容的位置"></a>3.7 容器中内容的位置</h4><p>justify-content 属性是整个内容区域在容器里面的水平位置（左中右），align-content 属性是整个内容区域的垂直位置（上中下）。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.container &#123;</span><br><span class="line">  justify-content: start | end | center | stretch | space-around | space-between | space-evenly;</span><br><span class="line">  align-content: start | end | center | stretch | space-around | space-between | space-evenly;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>place-content 属性是 align-content 属性和 justify-content 属性的合并简写形式。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">place-content: &lt;align-content&gt; &lt;justify-content&gt;</span><br></pre></td></tr></table></figure>

<h3 id="4-设置网格布局-项目属性"><a href="#4-设置网格布局-项目属性" class="headerlink" title="4. 设置网格布局-项目属性"></a>4. 设置网格布局-项目属性</h3><h4 id="4-1-指定项目位置"><a href="#4-1-指定项目位置" class="headerlink" title="4.1 指定项目位置"></a>4.1 指定项目位置</h4><p>指定项目的四个边框，分别定位在哪根网格线。<br>grid-column-start 属性：左边框所在的垂直网格线<br>grid-column-end 属性：右边框所在的垂直网格线<br>grid-row-start 属性：上边框所在的水平网格线<br>grid-row-end 属性：下边框所在的水平网格线</p>
<p>例：左边框是第二根垂直网格线，右边框是第四根垂直网格线。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.item-1 &#123;</span><br><span class="line">  grid-column-start: 2;</span><br><span class="line">  grid-column-end: 4;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>grid-column 属性是 grid-column-start 和 grid-column-end 的合并简写形式，grid-row 属性是 grid-row-start 属性和 grid-row-end 的合并简写形式。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.item &#123;</span><br><span class="line">  grid-column: &lt;start-line&gt; &#x2F; &lt;end-line&gt;;</span><br><span class="line">  grid-row: &lt;start-line&gt; &#x2F; &lt;end-line&gt;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-2-指定项目放在哪个区域"><a href="#4-2-指定项目放在哪个区域" class="headerlink" title="4.2 指定项目放在哪个区域"></a>4.2 指定项目放在哪个区域</h4><p>grid-area: e;</p>
<h4 id="4-3-指定单元格中内容位置-单个项目中"><a href="#4-3-指定单元格中内容位置-单个项目中" class="headerlink" title="4.3 指定单元格中内容位置(单个项目中)"></a>4.3 指定单元格中内容位置(单个项目中)</h4><p>justify-self 属性设置单元格内容的水平位置（左中右），跟 justify-items 属性的用法完全一致，但只作用于单个项目。</p>
<p>align-self 属性设置单元格内容的垂直位置（上中下），跟 align-items 属性的用法完全一致，也是只作用于单个项目。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.item &#123;</span><br><span class="line">  justify-self: start | end | center | stretch;</span><br><span class="line">  align-self: start | end | center | stretch;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>place-self 属性是 align-self 属性和 justify-self 属性的合并简写形式。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">place-self: &lt;align-self&gt; &lt;justify-self&gt;;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/myblog/images/wechat_channel.jpg">
          <span class="icon">
            <i class="fab fa-weixin"></i>
          </span>

          <span class="label">WeChat</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/myblog/tags/CSS/" rel="tag"># CSS</a>
              <a href="/myblog/tags/grid%E5%B8%83%E5%B1%80/" rel="tag"># grid布局</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2021/01/06/%E6%B7%B1%E6%B5%85%E6%8B%B7%E8%B4%9D/" rel="prev" title="深浅拷贝">
                  <i class="fa fa-chevron-left"></i> 深浅拷贝
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/myblog/2021/01/07/vue%E9%9D%A2%E8%AF%95%E9%A2%981/" rel="next" title="vue面试题1">
                  vue面试题1 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">匡宇迪</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script>

  
<script src="/myblog/js/local-search.js"></script>






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.css">

<script>
NexT.utils.loadComments('#gitalk-container', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '1926f8a1c453a1121d8d',
      clientSecret: '426722f9eef7599f8ebb3220379e704ea007a5f2',
      repo        : '',
      owner       : '',
      admin       : [''],
      id          : '523868c9ab6e10d691030a920100c3eb',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>
